<?php

class conf {

    public static $pkj_uid_comp = 0; //reservado
    public static $pkj_uid_interval = 0; //reservado
    public static $pkjHome = "/pkj";
    public static $pastaBase = "";
    public static $espelho = null;
    public static $lastError = null;

    public static function getEspelho() {
        $pasta = __DIR__ . "/../liteadm";
        conf::$espelho = array(
            "habilitado" => false,
            "servidor" => "sqlite",
            "endereco" => "$pasta/pkjsync.db",
            "usuario" => "",
            "senha" => "",
            "base" => "",
            "odbcDriver" => ""
        );
    }

    public static function getServidores() {
        $location = __DIR__.'/../banco.db';
        conf::$servidores = array(
            array(
                "servidor" => "sqlite",
                "endereco" => $location,
                "usuario" => "root",
                "senha" => "teste",
                "base" => "teste",
                "odbcDriver" => "",
                "espera" => array()
            )
        );
    }

    /**
     * USAR ESSA PODE CAUSAR A MORTE DO SEU CACHORRO
     * @param type $comando
     * @return boolean
     */
    public static function _pkjquery($comando) {
        conf::getEspelho();
        $habilitado = conf::$espelho["habilitado"];
        if (!$habilitado) {
            return false;
        }        
        $oo = true;
        $servidor = conf::$espelho["servidor"];
        $endereco = conf::$espelho["endereco"];
        $usuario = conf::$espelho["usuario"];
        $senha = conf::$espelho["senha"];
        $base = conf::$espelho["base"];
        $odbcDriver = conf::$espelho["odbcDriver"];
        $conexao = null;
        $retorno = null;
        $select = explode(" ", $comando);
        $isSelect = (strtolower($select[0]) == "select") ? TRUE : FALSE;
        if (strlen($odbcDriver) > 0) {
            $conexao = @odbc_connect("Driver={" . $odbcDriver . "};Server=" . $endereco . ";Database=" . $base . ";", $usuario, $senha);
        } else {
            switch ($servidor) {
                case "mysql":
                    $conexao = @mysql_connect($endereco, $usuario, $senha);
                    @mysql_select_db($base, $conexao);
                    break;
                case "postgree":
                    $conexao = @pg_connect("host=" . $endereco . " user=" . $usuario . " password=" . $senha . " dbname=" . $base);
                    break;
                case "mssql":
                    $conexao = @mssql_connect($endereco, $usuario, $senha);
                    break;
                case "sqlite":
                    try {
                        $conexao = new SQLite3($endereco);
                    } catch (Exception $exc) {
                        $retorno = false;
                    }
                    break;
            }
        }
        if (strlen($odbcDriver) > 0) {
            $retorno = odbc_exec($conexao, $comando);
        } else {
            switch ($servidor) :
                case "mysql":
                    $retorno = @mysql_query($comando, $conexao);
                    break;
                case "postgree":
                    $retorno = @pg_query($conexao, $comando);
                    break;
                case "mssql":
                    $retorno = @mssql_query($comando, $conexao);
                    break;
                case "sqlite":
                    $select = explode(" ", $comando);
                    if ($isSelect) {
                        $retorno = $conexao->query($comando);
                    } else {
                        $retorno = $conexao->exec($comando);
                    }
                    break;
            endswitch;
        }
        //obter os dados
        $query = $retorno;
        $retorno = array();
        if ($isSelect) {
            if (strlen($odbcDriver) > 0) {
                while ($row = ($oo) ? @odbc_fetch_object($query) : @odbc_fetch_assoc($query)) {
                    $retorno[] = $row;
                }
            } else {
                switch ($servidor) {
                    case "mysql":
                        while ($row = ($oo) ? @mysql_fetch_object($query) : @mysql_fetch_assoc($query)) {
                            $retorno[] = $row;
                        }
                        break;
                    case "postgree":
                        while ($row = ($oo) ? @pg_fetch_object($query) : @pg_fetch_assoc($query)) {
                            $retorno[] = $row;
                        }
                        break;
                    case "mssql":
                        while ($row = ($oo) ? @mssql_fetch_object($query) : @mssql_fetch_array($query)) {
                            $retorno[] = $row;
                        }
                        break;
                    case "sqlite":
                        if ($oo) {
                            while ($row = $query->fetchArray()) {
                                $obj = new stdClass();
                                foreach ($row as $key => $value) {
                                    if (!is_numeric($key)) {
                                        $obj->$key = $value;
                                    }
                                }
                                $retorno[] = $obj;
                            }
                        } else {
                            while ($row = $query->fetchArray()) {
                                $retorno[] = $row;
                            }
                        }
                        break;
                }
            }
            return ($retorno);
        } else {
            return $query;
        }
    }

    public static function getEspera($index) {
        $retorno = array();
        foreach (conf::_pkjquery("select CMD from CMD where LINK = '$index'") as $value) {
            $retorno[] = $value->CMD;
        }
        return $retorno;
    }

    public static function clearEspera($index) {
        conf::_pkjquery("delete from CMD where LINK='$index'");
    }

    public static function addEspera($index, $cmd) {
        conf::getEspelho();
        if(ucase(conf::$espelho["servidor"]) == "SQLITE"){
         $cmd = str_replace("'","''",$cmd);   
        }else{
         $cmd = addslashes($cmd);   
        }        
        conf::_pkjquery("insert into CMD (LINK,CMD) values('$index','$cmd')");
    }

    public static $servidores = array();
    public static $ativo = 0;
    public static $servidor = "sqlite";
    public static $endereco = "";
    public static $usuario = "webpkj";
    public static $senha = "webpkj";
    public static $base = "webpkj";
    public static $odbcDriver = ""; //if you like use odbc
    public static $espera = array(array());
//Sistema
    public static $pkj_bd_sis_conexao = null;
    public static $pkj_bd_sis_tipo = null;

//  conf::$servidor = (conf::$servidor == "") ? $servidor : conf::$servidor;
//  conf::$endereco = (conf::$endereco == "") ? $endereco : conf::$endereco;
//  conf::$usuario = (conf::$usuario == "") ? $usuario : conf::$usuario;
//  conf::$senha = (conf::$senha == "") ? $senha : conf::$senha;
//  conf::$base = (conf::$base == "") ? $base : conf::$base;
    public static function conectar($index) {
        conf::getServidores();
        conf::$servidor = conf::$servidores[$index]["servidor"];
        conf::$endereco = conf::$servidores[$index]["endereco"];
        conf::$usuario = conf::$servidores[$index]["usuario"];
        conf::$senha = conf::$servidores[$index]["senha"];
        conf::$base = conf::$servidores[$index]["base"];
        conf::$odbcDriver = conf::$servidores[$index]["odbcDriver"];
        conf::$espera = conf::$servidores[$index]["espera"];
        if (conf::$endereco == "") {
            return;
        }
        if (strlen(conf::$odbcDriver) > 0) {
            conf::$pkj_bd_sis_conexao[$index] = @odbc_connect("Driver={" . conf::$odbcDriver . "};Server=" . conf::$endereco . ";Database=" . conf::$base . ";", conf::$usuario, conf::$senha);
        } else {
            switch (conf::$servidor) {
                case "mysql":
                    conf::$pkj_bd_sis_conexao[$index] = @mysql_connect(conf::$endereco, conf::$usuario, conf::$senha);
                    @mysql_select_db(conf::$base, conf::$pkj_bd_sis_conexao[$index]);
                    break;
                case "postgree":
                    conf::$pkj_bd_sis_conexao[$index] = @pg_connect("host=" . conf::$endereco . " user=" . conf::$usuario . " password=" . conf::$senha . " dbname=" . conf::$base);
                    break;
                case "mssql"://tem que ser odbc ?
                    conf::$pkj_bd_sis_conexao[$index] = @mssql_connect(conf::$endereco, conf::$usuario, conf::$senha); //odbc_connect("Driver={SQL Server Native Client 10.0};Server=conf::$endereco;Database=conf::$base;", conf::$usuario, conf::$senha);
                    break;
                case "sqlite":
                    try {
                        conf::$pkj_bd_sis_conexao[$index] = new SQLite3(conf::$endereco);
                        // = new SQLite3(""
                    } catch (Exception $exc) {
//                       echo $exc->getTraceAsString();                        
                    }
                    break;
            }
        }
    }

    public static function testServer($indice) {
        $servidor = conf::$servidores [$indice]["servidor"];
        $endereco = conf::$servidores [$indice]["endereco"];
        $usuario = conf::$servidores [$indice]["usuario"];
        $senha = conf::$servidores [$indice]["senha"];
        $base = conf::$servidores [$indice]["base"];
        $odbcDriver = conf::$servidores[$indice]["odbcDriver"];
        $espera = conf::$servidores [$indice]["espera"];
        $p = true;
        if (strlen($odbcDriver) > 0) {
            $p = @odbc_connect("Driver={" . $odbcDriver . "};Server=" . $endereco . ";Database=" . $base . ";", $usuario, $senha);
        } else {
            switch ($servidor) {
                case "mysql":
                    $p = @mysql_connect($endereco, $usuario, $senha);
                    //mysql_select_db(conf::$base, conf::$pkj_bd_sis_conexao);
                    break;
                case "postgree":
                    $p = @pg_connect("host=" . $endereco . " user=" . $usuario . " password=" . $senha . " dbname=" . $base);
                    break;
                case "mssql"://tem que ser odbc ?
                    $p = @mssql_connect($endereco, $usuario, $senha); //odbc_connect("Driver={SQL Server Native Client 10.0};Server=conf::$endereco;Database=conf::$base;", conf::$usuario, conf::$senha);
                    break;
                case "sqlite":
                    $p = new SQLite3($endereco);
                    break;
            }
        }
        return $p;
    }

    public static function _trocaLink() {
        if ((count(conf::$servidores) - 1) == conf::$ativo) {
            conf::$ativo = 0;
        } else {
            conf::$ativo ++;
        }
    }

}

?>
